package com.ssly.media.manage.service;

import com.ssly.media.manage.domain.SourceMedia;
import com.ssly.media.manage.domain.TargetMedia;
import com.ssly.media.manage.ffmpeg.Media;
import com.ssly.media.manage.repository.SourceMediaRepository;
import com.ssly.media.manage.repository.TargetMediaRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.File;
import java.sql.Timestamp;

@Service
public class TargetMediaService {
    private final TargetMediaRepository targetMediaRepository;


    @Autowired
    public TargetMediaService(TargetMediaRepository targetMediaRepository) {
        this.targetMediaRepository = targetMediaRepository;
    }


    public TargetMedia create(File mediaFile, String name){
        Media media = new Media(mediaFile);
        TargetMedia targetMedia = new TargetMedia();
        targetMedia.setName(name);
        targetMedia.setSavePath(mediaFile.getAbsolutePath());
        targetMedia.setAcodec(media.getAcodec());
        targetMedia.setVcodec(media.getVcodec());
        targetMedia.setBitrate(media.getBitrate());
        targetMedia.setDuration(media.getDuration());
        targetMedia.setWidth(media.getWidth());
        targetMedia.setHeight(media.getHeight());
        targetMedia.setFps(media.getFps());
        targetMedia.setLenght(media.getLenght());
        targetMedia.setCreateTime(new Timestamp(System.currentTimeMillis()));
        return  targetMediaRepository.save(targetMedia);
    }
}
